﻿using System;
using System.Windows.Automation;
using ProdControls.UIAPatterns;
using ProdControls.Utility;

namespace ProdUi
{
    public static class Assert
    {
        public static void IsEqual(object one, object two, string message = "")
        {
            if (!one.Equals(two)) throw new Exception(message);
            
        }

        public static void IsPropertyEqual(AutomationProperty property,AutomationElement element, string expected, string message="")
        {
            object actual = element.GetCurrentPropertyValue(property);
            if (String.Compare(actual.ToString(), expected, StringComparison.Ordinal) != 0) throw new AssertionException(message);
        }

        public static void IsValueEqual(object one, object two, string message = "")
        {
            if (String.Compare(one.ToString(), two.ToString(), StringComparison.Ordinal) != 0) throw new AssertionException(message);
        }

        /// <summary>
        /// Determines whether the specified element is checked.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <exception cref="AssertionException">Throws an assert if item is checked</exception>
        public static void IsChecked(AutomationElement element)
        {
            ToggleState state = TogglePatternHelper.GetToggleState(element);
            if (state == ToggleState.On) throw new AssertionException();
        }

        /// <summary>
        /// Determines whether the specified element is checked.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <exception cref="AssertionException">Throws an assert if item is not checked</exception>
        public static void IsNotChecked(AutomationElement element)
        {
            ToggleState state = TogglePatternHelper.GetToggleState(element);
            if (state == ToggleState.Off) throw new AssertionException();
        }

        public static void IsSelected(AutomationElement element)
        {
            if (SelectionItemPatternHelper.IsItemSelected(element)) throw new AssertionException();
        }

        /// <summary>
        /// Determines whether the specified element contains item.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="itemtext">The items text.</param>
        /// <exception cref="ProdUi.AssertionException">Thrown if list contains the specified item text</exception>
        public static void ContainsItem(AutomationElement element, string itemtext)
        {
            if(SelectionItemPatternHelper.FindItemByText(element,itemtext) != null) throw new AssertionException();
        }

        /// <summary>
        /// Does the list not contain item.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="itemtext">The item text.</param>
        /// <exception cref="ProdUi.AssertionException">Thrown if list does not contain the specified item text</exception>
        public static void DoesNotContainItem(AutomationElement element, string itemtext)
        {
            if(SelectionItemPatternHelper.FindItemByText(element,itemtext) == null) throw new AssertionException();
            
        }

        /// <summary>
        /// Determines if the specified item is at the expected index
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="expectedIndex">The expected index.</param>
        /// <param name="itemText">The item text.</param>
        /// <exception cref="ProdUi.AssertionException">If the item is not at the expected index</exception>
        public static void ItemNotAtIndex(AutomationElement element, int expectedIndex, string itemText)
        {
            AutomationElement testElement = SelectionItemPatternHelper.FindItemByIndex(element, expectedIndex);
            if (String.Compare(testElement.Current.Name, itemText, StringComparison.Ordinal) != 0) throw new AssertionException();
        }

        /// <summary>
        /// Determines if a window with the specified title exists
        /// </summary>
        /// <param name="windowTitle">The window title.</param>
        /// <exception cref="AssertionException">Window Exists</exception>
        public static void WindowExists(string windowTitle)
        {
           if(Utilities.FindWindowPartial(windowTitle) != IntPtr.Zero) throw new AssertionException("Window Exists");
        }

        /// <summary>
        /// Determines if a window with the specified title exists
        /// </summary>
        /// <param name="windowTitle">The window title.</param>
        /// <exception cref="AssertionException">Window Does Not Exist</exception>
        public static void WindowDoesNotExist(string windowTitle)
        {
            if (Utilities.FindWindowPartial(windowTitle) != IntPtr.Zero) throw new AssertionException("Window Does Not Exist");
        }

    }
}
